package com.yudylaw.algorithm;

public class InsertSort {

	public static void main(String[] args) {
		int[] nums = {2,10,12,3,1,8,20,23,100,13,9,5,0};
		insertSort2(nums);
		for(int k=0;k<nums.length;k++){
			System.out.print(nums[k]+",");
		}
	}
	
	public static void insertSort(int [] sorts){
		for(int i=1;i<sorts.length;i++){
			int temp = sorts[i];
			for(int j=i-1;j>=0;j--){
				if(sorts[j] > temp ){
					sorts[j+1] = sorts[j];
					if(j==0)sorts[j] = temp;
				}else{
					sorts[j+1] = temp;
					break;
				}
			}
		}
	}
	
	public static void insertSort2(int [] sorts){
		for(int out=1;out<sorts.length;out++){
			int temp = sorts[out];
			int in = out;
			while(in>0 && sorts[in-1]>temp){
				sorts[in] = sorts[in-1];
				in--;
			}
			sorts[in] = temp;
		}
	}
}
